package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.FileBorrow;
import com.amsp.oa.oademo.service.FileBorrowService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "文件借阅管理")
@RestController
@RequestMapping("/api/fileBorrow")
public class FileBorrowController {

    @Autowired
    private FileBorrowService fileBorrowService;

    @ApiOperation("获取文件借阅列表")
    @GetMapping("/list")
    public ResponseEntity<IPage<FileBorrow>> getList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        Page<FileBorrow> page = new Page<>(current, size);
        return ResponseEntity.ok(fileBorrowService.getFileBorrowList(page, keyword));
    }

    @ApiOperation("获取文件借阅详情")
    @GetMapping("/{id}")
    public ResponseEntity<FileBorrow> getById(@PathVariable Long id) {
        return ResponseEntity.ok(fileBorrowService.getById(id));
    }

    @ApiOperation("保存文件借阅")
    @PostMapping("/save")
    public ResponseEntity<Boolean> save(@RequestBody List<FileBorrow> fileBorrow) {
        return ResponseEntity.ok(fileBorrowService.saveOrUpdateBatch(fileBorrow));
    }

    @ApiOperation("删除文件借阅")
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable Long id) {
        return ResponseEntity.ok(fileBorrowService.removeById(id));
    }
}